<template>
  <div class="dialog">
    <el-dialog
      :title="'设备类型' + titleText"
      :visible.sync="showDialog"
      width="35%"
      top="0"
      :close-on-click-modal="false"
      @close="cancel"
    >
      <el-form
        ref="ruleForm"
        label-position="left"
        :model="formData"
        :rules="rules"
        class="el-form"
      >
        <el-form-item label="设备类型名称：" prop="equipmentTypeName" class="w100p">
          <el-input
            v-model="formData.equipmentTypeName"
            placeholder="请输入设备类型名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="备注：" prop="remark" class="w100p">
          <el-input
            v-model="formData.remark"
            clearable
            :rows="2"
            type="textarea"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import equipTypeApi from "@/api/processing-management/equip-manage/equip-type/index";

export default {
  name: "",
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {};
      }
    },
    parentCode: {
      type: [String, Number],
      default: () => {
        return "";
      }
    }
  },
  data() {
    return {
      formData: {},
      rules: {
        equipmentTypeName: [
          {
            required: true,
            message: "请输入设备类型名称",
            trigger: ["change", "blur"]
          }
        ]
      },
      showDialog: this.show
    };
  },

  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === "add") {
        this.formData = {};
      }
      if (v === "edit") {
        this.formData = this.editObject;
      }
    }
  },
  created() {},
  mounted() {},
  methods: {
    save(formData) {
      this.$refs[formData].validate(async valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            // 编辑接口
            let params = {
              equipmentTypeCode: this.formData.equipmentTypeCode,
              remark: this.formData.remark,
              equipmentTypeName: this.formData.equipmentTypeName
            };
            params.parentCode = this.parentCode;
            equipTypeApi.edit(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success("修改成功！");
                this.close();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else if (this.nowChanceStau === "add") {
            const { data: res } = await equipTypeApi.queryBeOccupied(
              this.formData.equipmentTypeName
            );
            if (!res) {
              // 新增接口
              let params = {
                remark: this.formData.remark,
                equipmentTypeName: this.formData.equipmentTypeName
              };
              params.parentCode = this.parentCode;
              equipTypeApi.add(params).then(res => {
                if (res.code === 1) {
                  this.$message.success(res.msg);
                  this.close();
                } else {
                  this.$message.error(res.msg);
                }
              });
            } else {
              this.$message.error("该类型名称已存在，请重新命名！");
            }
          } else {
            this.$message.error("请选择父级类型，有且仅有一个！");
          }
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped>
</style>
